package com.chukun.spring.beans.defination;

import com.chukun.spring.beans.factory.DefaultUserFactory;
import com.chukun.spring.beans.factory.UserFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

/**
 * @author chukun
 *
 *  bean的初始化相关的生命周期流程
 */
@Configuration
public class BeanInitializationDemo {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        context.register(BeanInitializationDemo.class);

        context.refresh();
        System.out.println("spring context 已经启动.....");
        // 非延迟加载，在spring容器启动之后，就会初始化

        /**
         * UserFactory init.....
         * UserFactory InitializingBean interface ....
         * UserFactory initUserFactory .....
         * spring context 已经启动.....
         * com.chukun.spring.beans.factory.DefaultUserFactory@70b0b186
         */
        UserFactory bean = context.getBean(UserFactory.class);
        System.out.println(bean);

        // 延迟加载，依赖查找，触发初始化
        /**
         * lazy
         * spring context 已经启动.....
         * UserFactory init.....
         * UserFactory InitializingBean interface ....
         * UserFactory initUserFactory .....
         * com.chukun.spring.beans.factory.DefaultUserFactory@6a472554
         */
//        UserFactory bean = context.getBean(UserFactory.class);
//        System.out.println(bean);

        context.close();

    }

    @Bean(initMethod = "initUserFactory",destroyMethod = "userDefineDestory")
    // @Lazy
    public UserFactory userFactory(){
        return new DefaultUserFactory();
    }

}
